

import 'package:flutter/cupertino.dart';

class FrameAnimationImage extends StatefulWidget {
  final List<String> _assetList;
  final double width;
  final double height;
  bool start = true;
  int interval = 200;

  FrameAnimationImage(Key key, this._assetList,
      {required this.width, required this.height, required this.interval, required this.start})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return FrameAnimationImageState();
  }
}

class FrameAnimationImageState extends State<FrameAnimationImage>
    with SingleTickerProviderStateMixin {
  // 动画控制
  late Animation<double> _animation;
  late AnimationController _controller;
  int interval = 200;

  @override
  void initState() {
    super.initState();

    if (widget.interval != null) {
      interval = widget.interval;
    }
    final int imageCount = widget._assetList.length -1;
    final int maxTime = interval * imageCount;

    // 启动动画controller
    _controller = new AnimationController(duration: Duration(milliseconds: maxTime), vsync: this);
    // _controller.addStatusListener((AnimationStatus status) {
    //   if (status == AnimationStatus.completed) {
    //     _controller.forward(from: 0.0); // 完成后重新开始
    //   }
    // });

    _animation = new Tween<double>(begin: 0, end: imageCount.toDouble())
        .animate(_controller)..addListener(() {
      setState(() {
        // 这里改变的状态是动画对象的值
      });
    });
    if (widget.start) {
      _controller.forward();
    }
  }

  void startAnimation() => _controller.forward();
  void stopAnimation() => _controller.stop();
  void reStartAnimation(){
    _controller.reset();
    _controller.forward();
  }

  @override
  void didUpdateWidget(FrameAnimationImage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget called");
    if (widget.start) {
      _controller.forward();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    int ix = _animation.value.floor() % widget._assetList.length;

    List<Widget> images = [];
    // 把所有图片都加载进内容，否则每一帧加载时会卡顿
    for (int i = 0; i < widget._assetList.length; ++i) {
      if (i != ix) {
        images.add(Image.asset(widget._assetList[i],///ImageUtils只是一个简单出来图片格式的方法
          width: 0,
          height: 0,
        ));
      }
    }

    images.add(Image.asset(widget._assetList[ix],
      width: widget.width,
      height: widget.height,
    ));

    return Stack(alignment: AlignmentDirectional.center, children: images);
  }
}
//class ImageUtils {
//  static String getImagePath(String name, {String format: 'png'}) {
//    return 'assets/images/$name.$format';
//  }
//
//  static ImageProvider getImageProvider(String imageUrl,
//      {String holderImg: "none"}) {
//    if (TextUtil.isEmpty(imageUrl)) {
//      return AssetImage(getImagePath(holderImg));
//    }
//    return CachedNetworkImageProvider(imageUrl);
//  }
//}
